require(["config"], function () {
    require(["jquery", "template", "bootstrap", "common"], function ($, template) {
        function UserManage() {
            this.addListener();
            // this.loadUser();
            this.findAll();
        }

        UserManage.prototype = {
            addListener: function () {
                $(".btn-add-user").on("click", $.proxy(this.addUser, this))
            },
            addUser: function () {
                const url = "/api/users/register";
                const data = $(".form-register").serialize();
                $.get(url, data, function (data) {
                    if (data.res_body.status === 1) {
                        var html = "";

                        var data = {list:[data.res_body.data]},

                        html = template("user_template",data)

                        $(".users-body").append(html);
                        // sessionStorage.username = data.res_body.data.username;
                        // console.log(data.res_body.data.username);
                        // location.reload();
                        
                    }

                })
            },

            findAll: function(){
                const url = "/api/users/findAll";

                $.get(url,function(data){
                    var html = "";

                    var data = {list:data.res_body.data};

                    html = template("users_template",data);

                    $(".users-body").html(html);
                },"json")
            }

        }
        new UserManage();
    })
})